package com.vehicle.admin.web.Redis.opreation;

/**
 * Created by yantingbin on 2017/8/2.
 */
public class DiscountContext {
    private final double price;
    private final DiscountStrategy discountStrategy;

    public DiscountContext(DiscountStrategy discountStrategy,double price){
        new Thread(new EscapeRunnable()).start();
        this.price=price;
        this.discountStrategy=discountStrategy;
    }

    public double doDiscount(){
        return discountStrategy.discount(this.price);
    }

    public static void main(String[] args) {
        DiscountContext discountContext=new DiscountContext(new ChildDiscountStrategy(),100);

        System.out.println("after discount price is:"+discountContext.doDiscount());
    }
    private class EscapeRunnable implements Runnable {
        @Override
        public void run() {
            DiscountContext.this.doDiscount();
            // 在这里通过ThisEscape.this就可以引用外围类对象, 但是此时外围类对象可能还没有构造完成, 即发生了外围类的this引用的逃逸
        }
    }
}
